package com.lekang.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.lekang.domain.*;
import com.lekang.mapper.*;
import com.lekang.service.ITaskGenerationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 任务生成Service业务层处理
 * 
 * @author lekang
 * @date 2025-10-23
 */
@Service
public class TaskGenerationServiceImpl implements ITaskGenerationService 
{
    private static final Logger log = LoggerFactory.getLogger(TaskGenerationServiceImpl.class);

    @Autowired
    private TaskArrangementMapper taskArrangementMapper;
    
    @Autowired
    private CheckInManagementMapper checkInManagementMapper;
    
    @Autowired
    private NursingLevelMapper nursingLevelMapper;
    
    
    @Autowired
    private NursingItemPlanBindingMapper bindingMapper;
    
    @Autowired
    private OrderManagementMapper orderManagementMapper;
    
    @Autowired
    private ElderlyMapper elderlyMapper;

    @Autowired
    private CaregiverBedBindingMapper caregiverBedBindingMapper;

    // 任务分配计数器，用于轮流分配护理员
    private int taskAssignmentCounter = 0;

    /**
     * 为单个老人生成指定时间段内的护理任务
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int generateTasksForElderly(Long elderlyId, Date startDate, Date endDate) 
    {
        try {
            log.info("开始为老人ID: {} 生成任务，原始时间段: {} 至 {}", elderlyId, startDate, endDate);
            
            // ========== 重要：护理服务从入住次日开始 ==========
            // 将开始日期调整为次日 00:00:00
            Calendar adjustedStartCal = Calendar.getInstance();
            adjustedStartCal.setTime(startDate);
            adjustedStartCal.add(Calendar.DAY_OF_MONTH, 1); // 加1天
            adjustedStartCal.set(Calendar.HOUR_OF_DAY, 0);
            adjustedStartCal.set(Calendar.MINUTE, 0);
            adjustedStartCal.set(Calendar.SECOND, 0);
            adjustedStartCal.set(Calendar.MILLISECOND, 0);
            startDate = adjustedStartCal.getTime();
            
            log.info("护理服务实际开始日期调整为: {}", startDate);
            
            // 1. 查询老人的入住信息
            CheckInManagement checkIn = checkInManagementMapper.selectCheckInManagementByElder(elderlyId);
            if (checkIn == null) {
                log.warn("老人ID: {} 没有入住信息", elderlyId);
                return 0;
            }
            
            // 2. 查询护理等级关联的护理计划
            Long nursingLevelId = checkIn.getNursingLevelId();
            if (nursingLevelId == null) {
                log.warn("老人ID: {} 没有护理等级", elderlyId);
                return 0;
            }
            
            NursingLevel nursingLevel = nursingLevelMapper.selectNursingLevelById(nursingLevelId);
            if (nursingLevel == null || nursingLevel.getPlanId() == null) {
                log.warn("护理等级ID: {} 没有关联护理计划", nursingLevelId);
                return 0;
            }
            
            // 3. 查询护理计划下的所有护理项目（带执行周期）
            List<NursingItemPlanBinding> bindings = bindingMapper.selectByPlanId(nursingLevel.getPlanId());
            if (bindings == null || bindings.isEmpty()) {
                log.warn("护理计划ID: {} 没有关联护理项目", nursingLevel.getPlanId());
                return 0;
            }
            
            // 4. 根据执行周期生成任务列表
            List<TaskArrangement> tasks = new ArrayList<>();
            for (NursingItemPlanBinding binding : bindings) {
                log.info("处理护理项目绑定 - 护理项目ID: {}, 执行周期: {}, 执行频次: {}, 服务时间: {}", 
                    binding.getNursingItemId(), binding.getExecutionCycle(), 
                    binding.getExecutionFrequency(), binding.getServiceTime());
                
                List<Date> taskTimes = calculateTaskTimes(
                    startDate, 
                    endDate, 
                    binding.getExecutionCycle(),
                    binding.getExecutionFrequency(),
                    binding.getServiceTime()
                );
                
                log.info("计算出 {} 个任务时间点", taskTimes.size());
                
                for (Date taskTime : taskTimes) {
                    TaskArrangement task = new TaskArrangement();
                    task.setElderlyId(elderlyId);
                    task.setProjectNameId(binding.getNursingItemId());
                    task.setProjectType("护理计划内");
                    // 自动分配护理员：根据老人床位绑定的护理员进行轮流分配
                    Long assignedCaregiverId = assignCaregiverForElderly(elderlyId);
                    task.setCaregiverId(assignedCaregiverId);
                    task.setExpectedServiceTime(taskTime);
                    task.setCreator(1L); // 系统创建
                    task.setCreateTime(new Date());
                    task.setRelatedOrderNo(checkIn.getCheckInNo()); // 关联入住单号
                    task.setStatus("待执行");
                    tasks.add(task);
                }
            }
            
            // 5. 批量插入任务
            if (!tasks.isEmpty()) {
                for (TaskArrangement task : tasks) {
                    taskArrangementMapper.insertTaskArrangement(task);
                }
                log.info("成功为老人ID: {} 生成 {} 个任务", elderlyId, tasks.size());
                return tasks.size();
            }
            
            return 0;
        } catch (Exception e) {
            log.error("为老人ID: {} 生成任务失败", elderlyId, e);
            throw e;
        }
    }

    /**
     * 为所有在住老人生成月度任务（定时任务调用）
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int generateMonthlyTasksForAllElderly() 
    {
        try {
            log.info("开始生成月度任务...");
            
            // 1. 计算当月的开始和结束时间
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.DAY_OF_MONTH, 1);
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            Date startDate = cal.getTime();
            
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
            cal.set(Calendar.HOUR_OF_DAY, 23);
            cal.set(Calendar.MINUTE, 59);
            cal.set(Calendar.SECOND, 59);
            Date endDate = cal.getTime();
            
            // 2. 查询所有在住老人
            CheckInManagement query = new CheckInManagement();
            List<CheckInManagement> checkInList = checkInManagementMapper.selectCheckInManagementList(query);
            
            int totalTasks = 0;
            for (CheckInManagement checkIn : checkInList) {
                // 判断费用期限：如果合同本月到期，只生成到到期日
                Date actualEndDate = endDate;
                if (checkIn.getFeeEndDate() != null && checkIn.getFeeEndDate().before(endDate)) {
                    actualEndDate = checkIn.getFeeEndDate();
                }
                
                // 生成任务
                int count = generateTasksForElderly(checkIn.getElderlyId(), startDate, actualEndDate);
                totalTasks += count;
            }
            
            log.info("月度任务生成完成，共生成 {} 个任务", totalTasks);
            return totalTasks;
        } catch (Exception e) {
            log.error("生成月度任务失败", e);
            throw e;
        }
    }

    /**
     * 为订单生成护理计划外任务（微信下单后调用）
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int generateTaskForOrder(Long orderId) 
    {
        try {
            log.info("开始为订单ID: {} 生成任务", orderId);
            
            // 1. 查询订单信息
            OrderManagement order = orderManagementMapper.selectOrderManagementById(orderId);
            if (order == null) {
                log.warn("订单ID: {} 不存在", orderId);
                return 0;
            }
            
            // 2. 生成任务
            TaskArrangement task = new TaskArrangement();
            task.setElderlyId(order.getElderlyId());
            task.setProjectNameId(order.getNursingItemId());
            task.setProjectType("护理计划外");
            // 自动分配护理员：根据老人床位绑定的护理员进行轮流分配
            Long assignedCaregiverId = assignCaregiverForElderly(order.getElderlyId());
            task.setCaregiverId(assignedCaregiverId);
            task.setExpectedServiceTime(order.getExpectedServiceTime());
            task.setCreator(order.getOrderUserId()); // 下单人
            task.setCreateTime(new Date());
            task.setRelatedOrderNo(order.getOrderNo());
            task.setStatus("待执行");
            
            int result = taskArrangementMapper.insertTaskArrangement(task);
            log.info("成功为订单ID: {} 生成任务", orderId);
            return result;
        } catch (Exception e) {
            log.error("为订单ID: {} 生成任务失败", orderId, e);
            throw e;
        }
    }

    /**
     * 刷新任务状态（每日00:00:00执行）
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int refreshTaskStatus() 
    {
        try {
            log.info("开始刷新任务状态...");
            
            // 1. 查询所有待执行的任务
            TaskArrangement query = new TaskArrangement();
            query.setStatus("待执行");
            List<TaskArrangement> tasks = taskArrangementMapper.selectTaskArrangementList(query);
            
            int count = 0;
            for (TaskArrangement task : tasks) {
                // 2. 查询老人是否已退住（通过contractStatus字段判断）
                Elderly elderly = elderlyMapper.selectElderlyById(task.getElderlyId());
                if (elderly != null && "已退住".equals(elderly.getContractStatus())) {
                    // 3. 取消任务
                    task.setStatus("已取消");
                    task.setCancelTime(new Date());
                    task.setCancelPerson(1L); // 系统
                    task.setCancelReason("老人已退住");
                    taskArrangementMapper.updateTaskArrangement(task);
                    count++;
                }
            }
            
            log.info("任务状态刷新完成，共处理 {} 个任务", count);
            return count;
        } catch (Exception e) {
            log.error("刷新任务状态失败", e);
            throw e;
        }
    }

    /**
     * 为入住首月生成任务（入住完成后调用）
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int generateFirstMonthTasks(Long checkInId) 
    {
        try {
            log.info("开始为入住ID: {} 生成首月任务", checkInId);
            
            CheckInManagement checkIn = checkInManagementMapper.selectCheckInManagementById(checkInId);
            if (checkIn == null) {
                log.warn("入住ID: {} 不存在", checkInId);
                return 0;
            }
            
            // 生成从入住日到费用期限结束日的任务
            return generateTasksForElderly(
                checkIn.getElderlyId(),
                checkIn.getCheckInStartTime(),
                checkIn.getFeeEndDate()
            );
        } catch (Exception e) {
            log.error("为入住ID: {} 生成首月任务失败", checkInId, e);
            throw e;
        }
    }

    /**
     * 计算任务生成时间列表（核心算法）
     * 
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param cycle 执行周期（1:每日 2:每周 3:每月）
     * @param frequency 执行频次（每次执行的次数）
     * @param serviceTime 服务时间（格式：10:00）
     * @return 任务时间列表
     */
    private List<Date> calculateTaskTimes(Date startDate, Date endDate, Integer cycle, Integer frequency, String serviceTime) 
    {
        List<Date> taskTimes = new ArrayList<>();
        
        log.info("计算任务时间 - 执行周期: {}, 执行频次: {}, 服务时间: {}, 开始日期: {}, 结束日期: {}", 
            cycle, frequency, serviceTime, startDate, endDate);
        
        if (cycle == null || serviceTime == null || serviceTime.trim().isEmpty()) {
            log.warn("参数为空 - cycle: {}, serviceTime: {}", cycle, serviceTime);
            return taskTimes;
        }
        
        // 如果频次为空或小于1，默认为1次
        if (frequency == null || frequency < 1) {
            frequency = 1;
        }
        
        // 根据执行频次生成多个时间点（对于每日任务会生成一天内的多个时间）
        String[] times = generateTimesByFrequency(serviceTime, frequency, cycle);
        log.info("根据周期 {} 和频次 {} 生成的时间点数量: {}", cycle, frequency, times.length);
        
        try {
            if (cycle == 1) {
                // 每日任务：times数组包含一天内的多个时间点
                log.info("生成每日任务");
                taskTimes = generateDailyTasks(startDate, endDate, times);
            } else if (cycle == 2) {
                // 每周任务：根据frequency在一周内生成多次
                log.info("生成每周任务");
                taskTimes = generateWeeklyTasks(startDate, endDate, serviceTime, frequency);
            } else if (cycle == 3) {
                // 每月任务：根据frequency生成多次
                log.info("生成每月任务");
                taskTimes = generateMonthlyTasks(startDate, endDate, serviceTime, frequency);
            } else {
                log.warn("未知的执行周期: {}", cycle);
            }
            log.info("生成任务时间点数量: {}", taskTimes.size());
        } catch (ParseException e) {
            log.error("解析服务时间失败: {}", serviceTime, e);
        }
        
        return taskTimes;
    }

    /**
     * 生成每日任务时间
     */
    private List<Date> generateDailyTasks(Date startDate, Date endDate, String[] times) throws ParseException 
    {
        List<Date> taskTimes = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        cal.setTime(startDate);
        
        while (!cal.getTime().after(endDate)) {
            for (String time : times) {
                String timeStr = time.trim();
                
                // 解析时间（支持 HH:mm:ss 和 HH:mm 格式）
                SimpleDateFormat sdf;
                if (timeStr.length() > 5 && timeStr.split(":").length == 3) {
                    sdf = new SimpleDateFormat("HH:mm:ss");
                } else {
                    sdf = new SimpleDateFormat("HH:mm");
                }
                
                Date timeOfDay = sdf.parse(timeStr);
                Calendar taskCal = Calendar.getInstance();
                taskCal.setTime(cal.getTime());
                
                Calendar timeCal = Calendar.getInstance();
                timeCal.setTime(timeOfDay);
                taskCal.set(Calendar.HOUR_OF_DAY, timeCal.get(Calendar.HOUR_OF_DAY));
                taskCal.set(Calendar.MINUTE, timeCal.get(Calendar.MINUTE));
                taskCal.set(Calendar.SECOND, 0);
                taskCal.set(Calendar.MILLISECOND, 0);
                
                if (!taskCal.getTime().after(endDate)) {
                    taskTimes.add(taskCal.getTime());
                }
            }
            cal.add(Calendar.DAY_OF_MONTH, 1);
        }
        
        return taskTimes;
    }

    /**
     * 生成每周任务时间
     * 规则：每周生成指定频次的任务，一个月按4周计算
     * 例如：每周2次 × 4周 = 8次任务/月
     * 一周内的多次任务会分散在不同天（如周一、周四）
     */
    private List<Date> generateWeeklyTasks(Date startDate, Date endDate, String serviceTime, int frequency) throws ParseException 
    {
        List<Date> taskTimes = new ArrayList<>();
        final int WEEKS_PER_MONTH = 4; // 每月固定4周
        
        log.info("生成每周任务 - 每周{}次，共{}周", frequency, WEEKS_PER_MONTH);
        
        // 计算一周内任务的间隔天数
        int[] dayOffsetsInWeek = calculateWeeklyDayOffsets(frequency);
        
        // 对每周的每次任务
        for (int weekIndex = 0; weekIndex < WEEKS_PER_MONTH; weekIndex++) {
            for (int freqIndex = 0; freqIndex < frequency; freqIndex++) {
                String timeStr = serviceTime.trim();
                
                // 解析时间（支持 HH:mm:ss 和 HH:mm 格式）
                SimpleDateFormat sdf;
                if (timeStr.length() > 5 && timeStr.split(":").length == 3) {
                    sdf = new SimpleDateFormat("HH:mm:ss");
                } else {
                    sdf = new SimpleDateFormat("HH:mm");
                }
                Date timeOfDay = sdf.parse(timeStr);
                
                Calendar taskCal = Calendar.getInstance();
                taskCal.setTime(startDate);
                
                // 计算任务日期：起始日期 + 周偏移 + 周内天偏移
                taskCal.add(Calendar.WEEK_OF_YEAR, weekIndex); // 加上周数
                taskCal.add(Calendar.DAY_OF_WEEK, dayOffsetsInWeek[freqIndex]); // 加上周内偏移
                
                // 设置时间
                Calendar timeCal = Calendar.getInstance();
                timeCal.setTime(timeOfDay);
                taskCal.set(Calendar.HOUR_OF_DAY, timeCal.get(Calendar.HOUR_OF_DAY));
                taskCal.set(Calendar.MINUTE, timeCal.get(Calendar.MINUTE));
                taskCal.set(Calendar.SECOND, 0);
                taskCal.set(Calendar.MILLISECOND, 0);
                
                // 检查任务日期是否超过结束日期
                if (taskCal.getTime().after(endDate)) {
                    continue; // 超过结束日期则跳过
                }
                
                taskTimes.add(taskCal.getTime());
                log.debug("生成每周任务 - 第{}周第{}次: {}", weekIndex + 1, freqIndex + 1, taskCal.getTime());
            }
        }
        
        return taskTimes;
    }
    
    /**
     * 计算每周任务在一周内的天数偏移
     * 例如：每周2次 -> [0, 3] (周一和周四)
     *       每周3次 -> [0, 2, 4] (周一、周三、周五)
     */
    private int[] calculateWeeklyDayOffsets(int frequency) {
        if (frequency <= 1) {
            return new int[]{0}; // 每周1次，在起始日
        }
        
        int[] offsets = new int[frequency];
        
        if (frequency == 2) {
            offsets[0] = 0; // 周一（或起始日）
            offsets[1] = 3; // 周四（间隔3天）
        } else if (frequency == 3) {
            offsets[0] = 0; // 周一
            offsets[1] = 2; // 周三
            offsets[2] = 4; // 周五
        } else if (frequency == 4) {
            offsets[0] = 0; // 周一
            offsets[1] = 2; // 周三
            offsets[2] = 4; // 周五
            offsets[3] = 6; // 周日
        } else {
            // 更多次数：在7天内均匀分布
            int interval = 7 / frequency;
            for (int i = 0; i < frequency; i++) {
                offsets[i] = i * interval;
                if (offsets[i] > 6) offsets[i] = 6; // 最多到周日
            }
        }
        
        return offsets;
    }

    /**
     * 生成每月任务时间
     * 规则：根据频次生成任务（对应月度费用计算基准）
     * 在开始日期及后续日期生成任务
     */
    private List<Date> generateMonthlyTasks(Date startDate, Date endDate, String serviceTime, int frequency) throws ParseException 
    {
        List<Date> taskTimes = new ArrayList<>();
        
        log.info("生成每月任务 - 频次{}次", frequency);
        
        String timeString = serviceTime.trim();
        
        // 解析时间（支持 HH:mm:ss 和 HH:mm 格式）
        SimpleDateFormat sdf;
        if (timeString.length() > 5 && timeString.split(":").length == 3) {
            sdf = new SimpleDateFormat("HH:mm:ss");
        } else {
            sdf = new SimpleDateFormat("HH:mm");
        }
        Date timeOfDay = sdf.parse(timeString);
        
        // 根据频次生成多次任务（在一个月内均匀分布）
        int dayInterval = 30 / Math.max(1, frequency); // 任务间隔天数
        
        for (int i = 0; i < frequency; i++) {
            Calendar taskCal = Calendar.getInstance();
            taskCal.setTime(startDate);
            taskCal.add(Calendar.DAY_OF_MONTH, i * dayInterval); // 加上天数偏移
            
            Calendar timeCal = Calendar.getInstance();
            timeCal.setTime(timeOfDay);
            taskCal.set(Calendar.HOUR_OF_DAY, timeCal.get(Calendar.HOUR_OF_DAY));
            taskCal.set(Calendar.MINUTE, timeCal.get(Calendar.MINUTE));
            taskCal.set(Calendar.SECOND, 0);
            taskCal.set(Calendar.MILLISECOND, 0);
            
            // 检查任务日期是否超过结束日期
            if (!taskCal.getTime().after(endDate)) {
                taskTimes.add(taskCal.getTime());
                log.debug("生成每月任务 - 第{}次: {}", i + 1, taskCal.getTime());
            }
        }
        
        return taskTimes;
    }

    /**
     * 解析周几
     */
    private int parseDayOfWeek(String day) 
    {
        Map<String, Integer> dayMap = new HashMap<>();
        dayMap.put("周日", Calendar.SUNDAY);
        dayMap.put("周一", Calendar.MONDAY);
        dayMap.put("周二", Calendar.TUESDAY);
        dayMap.put("周三", Calendar.WEDNESDAY);
        dayMap.put("周四", Calendar.THURSDAY);
        dayMap.put("周五", Calendar.FRIDAY);
        dayMap.put("周六", Calendar.SATURDAY);
        
        return dayMap.getOrDefault(day, Calendar.MONDAY);
    }

    /**
     * 根据老人ID获取其床位绑定的护理员，并采用轮流分配策略返回一个护理员ID
     * 
     * @param elderlyId 老人ID
     * @return 分配的护理员ID，如果没有绑定护理员则返回null
     */
    private Long assignCaregiverForElderly(Long elderlyId) 
    {
        try {
            // 1. 查询老人信息，获取床位ID
            Elderly elderly = elderlyMapper.selectElderlyById(elderlyId);
            if (elderly == null || elderly.getBedId() == null) {
                log.debug("老人ID: {} 没有床位信息", elderlyId);
                return null;
            }
            
            Long bedId = elderly.getBedId();
            
            // 2. 查询该床位绑定的所有护理员ID
            List<Long> caregiverIds = caregiverBedBindingMapper.selectCaregiverIdsByBedId(bedId);
            
            if (caregiverIds == null || caregiverIds.isEmpty()) {
                log.debug("床位ID: {} 没有绑定护理员", bedId);
                return null;
            }
            
            // 3. 采用轮流分配策略：使用计数器取模
            Long assignedCaregiverId = caregiverIds.get(taskAssignmentCounter % caregiverIds.size());
            taskAssignmentCounter++; // 递增计数器
            
            log.debug("为老人ID: {} 分配护理员ID: {}", elderlyId, assignedCaregiverId);
            return assignedCaregiverId;
            
        } catch (Exception e) {
            log.error("为老人ID: {} 分配护理员失败", elderlyId, e);
            return null;
        }
    }

    /**
     * 根据执行频次生成多个时间点（仅用于每日任务）
     * 对于每周/每月任务，频次表示在周期内的执行次数，不是一天内的多次
     * 
     * @param baseTime 基准时间（格式：08:00:00 或 08:00）
     * @param frequency 执行频次
     * @param cycle 执行周期（1:每日 2:每周 3:每月）
     * @return 时间点数组
     */
    private String[] generateTimesByFrequency(String baseTime, int frequency, int cycle) {
        // 只有每日任务才会在一天内有多个时间点
        if (cycle != 1 || frequency <= 1) {
            return new String[]{baseTime};
        }
        
        String[] times = new String[frequency];
        
        try {
            // 解析基准时间（提取小时）
            String[] parts = baseTime.split(":");
            int baseHour = Integer.parseInt(parts[0]);
            
            if (frequency == 2) {
                // 2次：早晚（如8:00, 18:00）
                times[0] = String.format("%02d:00:00", Math.max(6, baseHour)); // 早上最早6点
                times[1] = "18:00:00"; // 晚上6点
            } else if (frequency == 3) {
                // 3次：早中晚（如8:00, 12:00, 18:00）
                times[0] = String.format("%02d:00:00", Math.max(6, baseHour)); // 早上
                times[1] = "12:00:00"; // 中午12点
                times[2] = "18:00:00"; // 晚上6点
            } else if (frequency == 4) {
                // 4次：早中晚加宵夜（如8:00, 12:00, 18:00, 21:00）
                times[0] = String.format("%02d:00:00", Math.max(6, baseHour));
                times[1] = "12:00:00";
                times[2] = "18:00:00";
                times[3] = "21:00:00";
            } else {
                // 更多次：在工作时间内均匀分布（6:00-22:00）
                int startHour = 6;
                int endHour = 22;
                int interval = (endHour - startHour) / (frequency - 1);
                
                for (int i = 0; i < frequency; i++) {
                    int hour = startHour + (i * interval);
                    if (hour > 22) hour = 22;
                    times[i] = String.format("%02d:00:00", hour);
                }
            }
            
            log.info("每日任务 - 基准时间 {} 扩展为 {} 个时间点: {}", baseTime, frequency, String.join(", ", times));
            
        } catch (Exception e) {
            log.error("解析时间失败: {}", baseTime, e);
            // 出错时返回原时间
            times[0] = baseTime;
        }
        
        return times;
    }
}

